<template>
  <div>
    <el-dialog title="新增员工" :visible="showDialog" @close="cancel">
      <el-form ref="addform" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" placeholder="请输入聘用形式">
            <el-option v-for="item in num " :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="formData.departmentName" placeholder="请输入部门" @focus="showTree = true" />
          <!-- 放置一个tree组件 -->
          <el-tree
            v-if="showTree"
            v-loading="loading"
            :data="depts"
            default-expand-all=""
            :props="{ label: 'name' }"
            @node-click="selectNode"
          />
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" style="width: 100%;" />
        </el-form-item>
      </el-form>
      <!-- 插槽 -->
      <template #footer>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" @click="isOk">确定</el-button>
            <el-button @click="cancel">取消</el-button>
          </el-col>

        </el-row>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import { addemployeeList } from '@/api/employees'
import { getDepts } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      num: [
        { id: 1, value: '正式员工' },
        { id: 2, value: '非正式员工' }
      ],
      depts: [],
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '请输入1-4位数字', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入姓名', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请正确输入手机号', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请输入聘用形式', trigger: 'blur' }],
        workNumber: [{ required: true, message: '请输入工号', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输入入职时间', trigger: 'blur' }]

      }

    }
  },
  //   获取用户部门信息处理成树形
  // 获取部门信息
  async created() {
    this.loading = true
    const res = await getDepts()
    this.depts = this.listToTree(res.depts, '')
    this.loading = false
  },
  methods: {
    // 单独封装一个递归函数把一维数组处理成多维
    listToTree(list, id) {
      const res = []
      list.forEach(element => {
        if (element.pid === id) {
          element.children = this.listToTree(list, element.id)
          res.push(element)
        }
      })
      return res
    },
    // 点击部门时触发
    selectNode(node) {
      // 将名字填充到输入框
      this.formData.departmentName = node.name
      // 隐藏
      this.showTree = false
    },

    //   点击确认时提交请求
    async isOk() {
      // 兜底校验
      this.$refs.addform.validate()
      await addemployeeList(this.formData)
      this.$message.success('提交成功')
      //   通知父组件关闭弹窗
      this.$emit('closedialog')
      // 通知父组件加载页面
      this.$emit('reload')
      // 清除数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }

      // 清除报错，重置数据
      this.$refs.addform.resetFields()
    },
    // 点击取消
    async cancel() {
      //   通知父组件关闭
      // 清除数据
      // 重置数据
      // 通知父组件加载页面
      this.$emit('closedialog')
      // 清除数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }

      // 清除报错，重置数据
      this.$refs.addform.resetFields()
    }
  }

}
</script>

<style>
.el-tree{
  position:absolute;
  z-index:9;
  height: 90px;
  width: 100%;
  overflow: auto;
}

</style>
